package com.bj.dljd.controller;

import cn.hutool.core.util.StrUtil;
import com.bj.dljd.convent.ConvertUserConvert;
import com.bj.dljd.convent.UserPayConfigConvent;
import com.bj.dljd.domain.UserInfo;
import com.bj.dljd.models.dto.UserLoginDto;
import com.bj.dljd.models.menus.SubMenu;
import com.bj.dljd.models.query.UserQuery;
import com.bj.dljd.models.response.R;
import com.bj.dljd.service.PmsOperatorService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {

    @Resource
    private PmsOperatorService pmsOperatorService;

    @Resource
    private ConvertUserConvert userPayConfigConvent;



    @RequestMapping("/operator/access")
    public R userLogin(@RequestBody @Valid UserQuery userQuery){

//        String md5Hex = DigestUtil.md5Hex(loginPwd);
        UserLoginDto userClient1 = pmsOperatorService.userLogin(userQuery);
        if (userClient1.getJwt() != null){
            return R.builder().code(200).msg("登录成功").data(userClient1).build();
        }
        return R.builder().code(401).msg(null).data("登录失败").build();
    }

    /**
     *   登录用户的菜单列表
     * @return
     */
    @GetMapping("/operator/menus")
    public R operator(@RequestHeader("operatorId") Long operatorId){
        if (operatorId != null && operatorId > 0){
        // 查询登录用户的id 查询它的菜单列表
            List<SubMenu> subMenuList = pmsOperatorService.queryMenus(operatorId);
            return R.Ok(subMenuList,"业务处理成功");
        }
        return R.fuil("登录信息有误");
    }
    /*模糊匹配商户名称*/
    @GetMapping("/user/matchshopname")
    public R matchShopName(@RequestParam String shopName){
        if (StrUtil.isNotBlank(shopName) && shopName.length() >= 2){
           List<UserInfo> userInfoList = pmsOperatorService.matchSopName(shopName);
           return R.Ok(userPayConfigConvent.convertListUserInfoVo(userInfoList));
        }
        return R.fuil("请求参数异常");
    }
}
